﻿using SAM.Framework.RepositoryDapper.NET40;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAM.Framework.Repository.Repositories;
using SAM.Framework.Repository.Repositories.Pager;
using SAM.Framework.Repository.Connection;
using SAM.Framework.RepositoryTest.NET40.Models;
using Dapper;

namespace SAM.Framework.RepositoryTest.NET40.CustomRepository
{
    public class CheckRecordRepository: DapperRepositoryBase<Models.CheckRecord>
    {
        public CheckRecordRepository(BaseDbContext dbContext) : base(dbContext, string.Empty)
        {

        }
        public override PageResult<CheckRecord> GetPage(PageOption<CheckRecord> option)
        {
            List<string> sortstrs = new List<string>();
            foreach(var item in option.SortInfos)
            {
                sortstrs.Add(string.Format("{0} {1}", item.FieldName, item.SortType.ToString()));
            }
            if (sortstrs.Count == 0)
            {
                sortstrs.Add(" id asc ");
            }
            string predicate = string.IsNullOrEmpty(option.Predicate) ? "1=1" : option.Predicate;
            string sqlSource = "select * from MonthCheckRecord201805 where " + predicate;

            string sql = string.Format(pageSql, option.PageSize, sqlSource,
                option.PageSize * (option.PageIndex - 1), string.Join(",", sortstrs),
                predicate);
            PageResult<CheckRecord> result = new PageResult<CheckRecord>();
            result.Total = this.Connection.ExecuteScalar<int>(string.Format("select count(*) from MonthCheckRecord201805 where {0}", predicate));
            result.PageIndex = option.PageIndex;
            result.PageSize = option.PageSize;
            result.Datas = this.Connection.Query<Models.CheckRecord>(sql);
            return result;
        }
    }
}
